home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / apps / 93 / applic / sdbcre.c < prev    next >
C/C++ Source or Header  |  1987-01-15  |  4KB  |  154 lines

  1. /* SDB - relation creation routines */
  2.  
  3. #include "sdbio.h"
  4.  
  5. /* external routines */
  6. extern long lseek();
  7.  
  8. /* db_rcreate(rname) - begin the creation of a new relation */
  9. struct relation *db_rcreate(rname)
  10.   char *rname;
  11. {
  12.     struct relation *rptr;
  13.  
  14.     /* allocate the relation structure */
  15.     if ((rptr = (struct relation *)calloc(1,sizeof(struct relation))) == NULL)
  16.     return (db_nerror(INSMEM));
  17.  
  18.     /* initialize the relation structure */
  19.     strncpy(rptr->rl_name,rname,RNSIZE);
  20.     rptr->rl_tcnt = 0;
  21.     rptr->rl_tmax = 0;
  22.     rptr->rl_text = 0;
  23.     rptr->rl_data = 512;
  24.     rptr->rl_size = 1;
  25.     rptr->rl_header.hd_attrs[0].at_name[0] = 0;
  26.  
  27.     /* return the new relation structure pointer */
  28.     return (rptr);
  29. }
  30.  
  31. /* db_rcheader - create the relation header */
  32. int db_rcheader(rptr,text)
  33.   struct relation *rptr; unsigned int text;
  34. {
  35.     char rname[RNSIZE+1],filename[RNSIZE+13];
  36.  
  37.     /* store the amount to extend by */
  38.     rptr->rl_text = text;
  39.  
  40.     /* initialize the relation file header */
  41.     db_cvbytes(rptr->rl_tcnt,rptr->rl_header.hd_tcnt);
  42.     db_cvbytes(rptr->rl_tmax,rptr->rl_header.hd_tmax);
  43.     db_cvbytes(rptr->rl_text,rptr->rl_header.hd_text);
  44.     db_cvbytes(rptr->rl_data,rptr->rl_header.hd_data);
  45.     db_cvbytes(rptr->rl_size,rptr->rl_header.hd_size);
  46.  
  47.     /* create the relation file name */
  48.     strncpy(rname,rptr->rl_name,RNSIZE); rname[RNSIZE] = 0;
  49.     sprintf(filename,"%s.sdb",rname);
  50.  
  51.     /* create the relation file */
  52.     if ((rptr->rl_fd = creat(filename,0)) == -1) {
  53.     free(rptr);
  54.     return (db_ferror(RELCRE));
  55.     }
  56.  
  57.     /* write the header to the relation file */
  58.     if (write(rptr->rl_fd,&rptr->rl_header,512) != 512) {
  59.     close(rptr->rl_fd);
  60.     free(rptr);
  61.     return (db_ferror(BADHDR));
  62.     }
  63.  
  64.     /* return successfully */
  65.     return (TRUE);
  66. }
  67.  
  68. /* db_rctuples - create the relation tuples */
  69. int db_rctuples(rptr,tcnt)
  70.   struct relation *rptr; unsigned int tcnt;
  71. {
  72.     unsigned int i;
  73.     char *tbuf;
  74.  
  75.     /* store the number of tuples */
  76.     rptr->rl_tmax = tcnt;
  77.  
  78.     /* allocate a tuple buffer */
  79.     if ((tbuf = calloc(1,rptr->rl_size)) == NULL)
  80.     return (db_ferror(INSMEM));
  81.  
  82.     /* write null tuples into the file */
  83.     for (i = 0; i < tcnt; i++)
  84.     if (write(rptr->rl_fd,tbuf,rptr->rl_size) != rptr->rl_size) {
  85.         free(tbuf);
  86.         return (db_ferror(INSBLK));
  87.     }
  88.  
  89.     /* free the tuple buffer */
  90.     free(tbuf);
  91.  
  92.     /* return successfully */
  93.     return (TRUE);
  94. }
  95.  
  96. /* db_rcdone(rptr) - finish the creation of a new relation */
  97. int db_rcdone(rptr)
  98.   struct relation *rptr;
  99. {
  100.     /* initialize the relation file header */
  101.     db_cvbytes(rptr->rl_tcnt,rptr->rl_header.hd_tcnt);
  102.     db_cvbytes(rptr->rl_tmax,rptr->rl_header.hd_tmax);
  103.  
  104.     /* write the header to the relation file */
  105.     if (lseek(rptr->rl_fd,0L,0) != 0L
  106.     ||  write(rptr->rl_fd,&rptr->rl_header,512) != 512) {
  107.     close(rptr->rl_fd);
  108.     free(rptr);
  109.     return (db_ferror(BADHDR));
  110.     }
  111.  
  112.    /* close the relation file */
  113.     close(rptr->rl_fd);
  114.  
  115.     /* free the relation structure */
  116.     free(rptr);
  117.  
  118.     /* return successfully */
  119.     return (TRUE);
  120. }
  121.  
  122. /* db_rcattr(rptr,aname,type,size) - add an attribute to relation being created */
  123. int db_rcattr(rptr,aname,type,size)
  124.   struct relation *rptr; char *aname; int type,size;
  125. {
  126.     int i;
  127.  
  128.     /* look for attribute name */
  129.     for (i = 0; i < NATTRS; i++)
  130.     if (rptr->rl_header.hd_attrs[i].at_name[0] == 0)
  131.         break;
  132.     else if (db_sncmp(aname,rptr->rl_header.hd_attrs[i].at_name,ANSIZE) == 0)
  133.         return (db_ferror(DUPATT));
  134.  
  135.     /* check for too many attributes */
  136.     if (i == NATTRS)
  137.     return (db_ferror(MAXATT));
  138.  
  139.     /* store the new attribute */
  140.     strncpy(rptr->rl_header.hd_attrs[i].at_name,aname,ANSIZE);
  141.     rptr->rl_header.hd_attrs[i].at_type = type;
  142.     rptr->rl_header.hd_attrs[i].at_size = size;
  143.  
  144.     /* terminate the attribute table */
  145.     if (++i != NATTRS)
  146.     rptr->rl_header.hd_attrs[i].at_name[0] = 0;
  147.  
  148.     /* update the tuple size */
  149.     rptr->rl_size += size;
  150.  
  151.     /* return successfully */
  152.     return (TRUE);
  153. }
  154.